package com.team.activity.exception;
import com.team.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 全局异常处理器
 */
@Slf4j
//@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public AjaxResult handleException(Exception e){
        log.error("程序出错啦~", e);
        return AjaxResult.error("出错啦, 请联系管理员~");
    }

    @ExceptionHandler
    public AjaxResult handleDuplicateKeyException(DuplicateKeyException e){
        log.error("程序出错啦~", e);
        String message = e.getMessage();
        int i = message.indexOf("Duplicate entry");
        String errMsg = message.substring(i);
        String[] arr = errMsg.split(" ");
        return AjaxResult.error( arr[2] + " 已存在");
    }
}
